//
//  ViewController.swift
//  DriverClock
//
//  Created by 童巍 on 17/1/11.
//  Copyright © 2017年 童巍. All rights reserved.
//

import UIKit

func lengthOfPx(px: CGFloat)->CGFloat{
   return ((px/1080.0)*UIScreen.main.bounds.size.width)
}

func systemFontOfpx(px:CGFloat) -> UIFont{

    return UIFont.systemFont(ofSize: ((px/2/96.0) * 72.0));
}


@available(iOS 10.0, *)
class ViewController: UIViewController, BMKMapViewDelegate, BMKLocationServiceDelegate, BMKGeoCodeSearchDelegate{
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var weekendLabel: UILabel!
    @IBOutlet weak var onWorkView: UIView!
    @IBOutlet weak var offWorkView: UIView!
    @IBOutlet weak var onWorkImage: UIImageView!
    @IBOutlet weak var offWorkImage: UIImageView!
    @IBOutlet weak var onWorkTime: UILabel!
//    @IBOutlet weak var onWorkLabel: UILabel!
    @IBOutlet weak var onWorkButton: UIButton!
    @IBOutlet weak var offWorkTime: UILabel!
//    @IBOutlet weak var offWorkLabel: UILabel!
    @IBOutlet weak var offWorkButton: UIButton!
    @IBOutlet weak var onWorkClockSuccessView: UIView!
    @IBOutlet weak var offWorkClockSuccessView: UIView!
    

    @IBOutlet weak var onWorkClockTime: UILabel!
    
    @IBOutlet weak var onWorkClockSuccessAddress: UILabel!
    
    @IBOutlet weak var offWorkClockTime: UILabel!
    @IBOutlet weak var offWorkClockAddress: UILabel!

    var alerWindow:AlertWindow?
    var locationServer:BMKLocationService?;
    var mapview:BMKMapView?;
    var isShowUserLocation:Bool? = true
    var coordina:CLLocationCoordinate2D?
    var geoCode:BMKGeoCodeSearch?
//    var mytimer:Timer?
    
    func getClockResult() {
        let date = WTMNetworkTool.getInternetDate()
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
       let time = formatter.string(from: date!)
        let startTime = time+" "+"00:00:00"
        let endTime = time+" "+"23:59:59"
        WTMNetworkTool.shared().request(withUrlstring: "app/attendance/attendanceHistory", method: "POST", parameters: ["params":["startTime":startTime,"endTime":endTime]], success: {[weak self](respone)->Void in
           print("\(respone)")
//          Tool.error(withMsg: (respone as? NSDictionary)?.object(forKey: "resultMsg") as! String!)
            let dic = (respone as? NSDictionary)?.object(forKey: "status");
            if ((dic as? NSNumber)?.intValue == 0){
                if ((respone as? NSDictionary)?.object(forKey: "result") as! NSArray).count > 0 {
                    let attendances = ((((respone as? NSDictionary)?.object(forKey: "result") as! NSArray)[0] as! NSDictionary).object(forKey: "attendances") as! NSArray)
                    for value in attendances{
                        if ((value as! NSDictionary).object(forKey: "type") as! integer_t) == 1{
                            self?.onWorkClockSuccessView.isHidden = false
                            let time = ((value as! NSDictionary).object(forKey:"time") as! double_t) / 1000.0
                            let formatter = DateFormatter()
                            formatter.dateFormat = "HH:mm"
                            self?.onWorkClockTime.text = "到岗:" + formatter.string(from: Date.init(timeIntervalSince1970: time))
                            self?.onWorkClockSuccessAddress.text = ((value as! NSDictionary).object(forKey:"address") as! String)
                        }
                        
                        if ((value as! NSDictionary).object(forKey: "type") as! integer_t) == 2{
                            self?.offWorkClockSuccessView.isHidden = false
                            let time = ((value as! NSDictionary).object(forKey:"time") as! double_t) / 1000.0
                            let formatter = DateFormatter()
                            formatter.dateFormat = "HH:mm"
                            self?.offWorkClockTime.text = "离岗:" + formatter.string(from: Date.init(timeIntervalSince1970: time))
                            self?.offWorkClockAddress.text = ((value as! NSDictionary).object(forKey:"address") as! String)
                        }

                    }
                
                }
            
            }
            
        }, failure: {(error)->Void in
        
        
        
        })
        
    }
    
    
    
    
    
    deinit{
//        mytimer?.invalidate()
    }
    
    func initTime(){
    
        let myCalendar:NSCalendar = NSCalendar.init(calendarIdentifier:NSCalendar.Identifier.gregorian)!
        myCalendar.locale = Locale.init(identifier: "zh_CN")
        myCalendar.timeZone = NSTimeZone(abbreviation: "EST")! as TimeZone
        myCalendar.timeZone = NSTimeZone(forSecondsFromGMT: +28800) as TimeZone
        let date = WTMNetworkTool.getInternetDate()
        
        let compoents:DateComponents = myCalendar.components([NSCalendar.Unit.year,NSCalendar.Unit.month,NSCalendar.Unit.day,NSCalendar.Unit.weekday,NSCalendar.Unit.hour,NSCalendar.Unit.minute], from: date!)
        self.dateLabel.text = String(compoents.year!) + "." + String(compoents.month!) + "." + String(compoents.day!)
        switch compoents.weekday! {
        case 1:self.weekendLabel.text = "星期日"
        case 2:self.weekendLabel.text = "星期一"
        case 3:self.weekendLabel.text = "星期二"
        case 4:self.weekendLabel.text = "星期三"
        case 5:self.weekendLabel.text = "星期四"
        case 6:self.weekendLabel.text = "星期五"
        case 7:self.weekendLabel.text = "星期六"
        default:
            break
        }
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm"
        
        self.onWorkTime.text = "09:00"
        self.offWorkTime.text = "18:00"

    
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        getClockResult()
        self.title = UserDefaults.standard.value(forKey: "stationName") as! String?
        self.navigationItem.rightBarButtonItem?.title = UserDefaults.standard.value(forKey: "userName") as! String?
        initTime()
       
//        mytimer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(self.initTime), userInfo: nil, repeats: true)
        
        
        
        
        geoCode = BMKGeoCodeSearch();
        
        self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil);
        locationServer = BMKLocationService();
        locationServer?.startUserLocationService();
        mapview = BMKMapView(frame: CGRect(x: 0, y: lengthOfPx(px: (1920 - 580)), width: self.view.frame.width, height: lengthOfPx(px: 580)))
        mapview?.showsUserLocation = true;
        self.view.addSubview(mapview!);
        self.view.removeConstraints(self.view.constraints);
        initUI();
    }
    func initUI() {
        dateLabel.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 50));
            make?.top.mas_equalTo()(lengthOfPx(px: 200));
            make?.right.mas_equalTo()(0);
            make?.height.mas_equalTo()(lengthOfPx(px: 144));
        }
        dateLabel.font = systemFontOfpx(px: 144);
        
        weekendLabel.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 50));
            make?.top.mas_equalTo()(self.dateLabel.mas_bottom)?.setOffset(lengthOfPx(px: 30));
            make?.right.mas_equalTo()(0);
            make?.height.mas_equalTo()(lengthOfPx(px: 144));
        }
        weekendLabel.font = systemFontOfpx(px: 144);
        
        onWorkView.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 50));
            make?.top.mas_equalTo()(self.weekendLabel.mas_bottom)?.setOffset(lengthOfPx(px: 40));
            make?.right.mas_equalTo()(lengthOfPx(px: -50));
            make?.height.mas_equalTo()(lengthOfPx(px: 360));
        }
        
        offWorkView.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 50));
            make?.top.mas_equalTo()(self.onWorkView.mas_bottom)?.setOffset(lengthOfPx(px: 30));
            make?.right.mas_equalTo()(lengthOfPx(px: -50));
            make?.height.mas_equalTo()(lengthOfPx(px: 360));
        }
        
        onWorkImage.mas_makeConstraints { (make) in
            make?.centerY.mas_equalTo()(self.onWorkView.mas_centerY);
            make?.width.mas_equalTo()(lengthOfPx(px: 320));
            make?.height.mas_equalTo()(lengthOfPx(px: 320));
            make?.left.mas_equalTo()(lengthOfPx(px: 30));
        }
        
        onWorkTime.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 430));
            make?.top.mas_equalTo()(self.onWorkView.mas_top)?.setOffset(lengthOfPx(px: 40));
            make?.right.mas_equalTo()(0)
            make?.height.mas_equalTo()(lengthOfPx(px: 144))
        }
        onWorkTime.font = systemFontOfpx(px: 144);
        
//        onWorkLabel.mas_makeConstraints { (make) in
//            make?.left.mas_equalTo()(lengthOfPx(px: 430));
//            make?.bottom.mas_equalTo()(lengthOfPx(px: -50))
//            make?.width.mas_equalTo()(lengthOfPx(px: 150))
//            make?.height.mas_equalTo()(lengthOfPx(px: 72))
//        }
//        onWorkLabel.font = UIFont.boldSystemFont(ofSize: (72/2/96.0) * 72.0);
        
        offWorkImage.mas_makeConstraints { (make) in
            make?.centerY.mas_equalTo()(self.offWorkView.mas_centerY);
            make?.width.mas_equalTo()(lengthOfPx(px: 320));
            make?.height.mas_equalTo()(lengthOfPx(px: 320));
            make?.left.mas_equalTo()(lengthOfPx(px: 30));
        }
        
        offWorkTime.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 430));
            make?.top.mas_equalTo()(self.offWorkView.mas_top)?.setOffset(lengthOfPx(px: 40));
            make?.right.mas_equalTo()(0)
            make?.height.mas_equalTo()(lengthOfPx(px: 144))
        }
        offWorkTime.font = systemFontOfpx(px: 144);
        
//        offWorkLabel.mas_makeConstraints { (make) in
//            make?.left.mas_equalTo()(lengthOfPx(px: 430));
//            make?.bottom.mas_equalTo()(lengthOfPx(px: -50))
//            make?.width.mas_equalTo()(lengthOfPx(px: 150))
//            make?.height.mas_equalTo()(lengthOfPx(px: 72))
//        }
//        offWorkLabel.font = UIFont.boldSystemFont(ofSize: (72/2/96.0) * 72.0);
        
        onWorkButton.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(lengthOfPx(px: -40))
            make?.bottom.mas_equalTo()(lengthOfPx(px: -40))
            make?.height.mas_equalTo()(lengthOfPx(px: 120))
            make?.width.mas_equalTo()(lengthOfPx(px: 240))
        }
        onWorkButton.titleLabel?.font = systemFontOfpx(px: 60);
        
        offWorkButton.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(lengthOfPx(px: -40))
            make?.bottom.mas_equalTo()(lengthOfPx(px: -40))
            make?.height.mas_equalTo()(lengthOfPx(px: 120))
            make?.width.mas_equalTo()(lengthOfPx(px: 240))
        }
        offWorkButton.titleLabel?.font = systemFontOfpx(px: 60);
        
        onWorkButton.layer.cornerRadius = lengthOfPx(px: 60);
        offWorkButton.layer.cornerRadius = lengthOfPx(px: 60);
   //clockview
        onWorkClockSuccessView.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(0);
            make?.bottom.mas_equalTo()(0);
            make?.left.mas_equalTo()(lengthOfPx(px: 430));
            make?.top.mas_equalTo()(self.onWorkButton.mas_top);
        }
        
        offWorkClockSuccessView.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(0);
            make?.bottom.mas_equalTo()(0);
            make?.left.mas_equalTo()(lengthOfPx(px: 430));
            make?.top.mas_equalTo()(self.offWorkButton.mas_top);
        }
//clockTimeAndAddress
        onWorkClockTime.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(0);
            make?.top.mas_equalTo()(0);
            make?.right.mas_equalTo()(0);
            make?.height.mas_equalTo()(lengthOfPx(px: 72))
        }
        onWorkClockTime.font = systemFontOfpx(px: 72);
        
        onWorkClockSuccessAddress.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(0);
            make?.top.mas_equalTo()(self.onWorkClockTime.mas_bottom)?.setOffset(lengthOfPx(px: 20));
            make?.right.mas_equalTo()(0);
            make?.bottom.mas_equalTo()(lengthOfPx(px: -30))

        }
        onWorkClockSuccessAddress.adjustsFontSizeToFitWidth = true;
        onWorkClockSuccessAddress.font = systemFontOfpx(px: 48)
        
        offWorkClockTime.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(0);
            make?.top.mas_equalTo()(0);
            make?.right.mas_equalTo()(0);
            make?.height.mas_equalTo()(lengthOfPx(px: 72))
        }
        offWorkClockTime.font = systemFontOfpx(px: 72);
        
        offWorkClockAddress.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(0);
            make?.top.mas_equalTo()(self.offWorkClockTime.mas_bottom)?.setOffset(lengthOfPx(px: 20));
            make?.right.mas_equalTo()(0);
            make?.bottom.mas_equalTo()(lengthOfPx(px: -30))
            
        }
        offWorkClockAddress.adjustsFontSizeToFitWidth = true
        offWorkClockAddress.font = systemFontOfpx(px: 48);
        
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        mapview?.delegate = self;
        locationServer?.delegate = self;
        geoCode?.delegate = self;
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated);
        mapview?.delegate = nil;
        locationServer?.delegate = nil;
        geoCode?.delegate = nil;
    }
    
    func didUpdate(_ userLocation: BMKUserLocation!) {
        mapview?.updateLocationData(userLocation);
        if isShowUserLocation! {
            self.mapview?.setRegion(BMKCoordinateRegionMake(userLocation.location.coordinate, BMKCoordinateSpanMake(0.038325, 0.028045)), animated: true);
            coordina = userLocation.location.coordinate
            isShowUserLocation = false;
        }
        
    }
    
    func chooseClockAddress() {
        
    }
    
    func reverseGeoCode(type:integer_t,completionHandler: @escaping (_ time:String,_ address:String) -> Void) {
        let reverseGeoCodeOption:BMKReverseGeoCodeOption?=BMKReverseGeoCodeOption()
        reverseGeoCodeOption!.reverseGeoPoint = (self.locationServer?.userLocation.location.coordinate)!
        
        if (geoCode?.reverseGeoCode(reverseGeoCodeOption))!{
            print("反检索成功")
            
        }else{
            print("反检索失败")
        }
        
        alerWindow = AlertWindow.init(frame: UIScreen.main.bounds)
        alerWindow?.windowLevel = UIWindowLevelAlert - 1;
        alerWindow?.makeKeyAndVisible()
        alerWindow?.addressView?.callback = completionHandler
        alerWindow?.addressView?.type = type
        alerWindow?.addressView?.locationDelegate = self
        
    }

    @IBAction func onWorkClick(_ sender: UIButton) {
        reverseGeoCode(type: 1, completionHandler: {[weak self](time, address) in
            self?.onWorkClockSuccessView.isHidden = !(self?.onWorkClockSuccessView.isHidden)!;
            self?.onWorkClockTime.text = "到岗:" + time
            self?.onWorkClockSuccessAddress.text = address
            })

    }
    
    @IBAction func offWorkClick(_ sender: UIButton) {
        if !self.onWorkClockSuccessView.isHidden {
            reverseGeoCode(type: 2, completionHandler: {[weak self](time, address) in
                self?.offWorkClockSuccessView.isHidden = !(self?.offWorkClockSuccessView.isHidden)!;
                self?.offWorkClockTime.text = "离岗:" + time
                self?.offWorkClockAddress.text = address
            })
        }else{
            Tool.error(withMsg: "未到岗不能离岗")
        
        }
        
    }
    
//Mark:geoCode
    func onGetReverseGeoCodeResult(_ searcher: BMKGeoCodeSearch!, result: BMKReverseGeoCodeResult!, errorCode error: BMKSearchErrorCode) {
        switch error {
        case BMK_SEARCH_NO_ERROR:
            alerWindow?.addressView?.dateSource = result.poiList!
            alerWindow?.addressView?.reloadData()
        default: break
            
        }
    }
    
    
}
//Mark:chooseAddress
typealias callBackFunc = (_ time:String,_ address:String)->Void
typealias hiddenCallBack = ()->Void

@available(iOS 10.0, *)
class chooseAddressTableView:UITableView,UITableViewDataSource,UITableViewDelegate,CAAnimationDelegate{
    var dateSource:Array<Any>? = nil;
    var callback:callBackFunc?
    var callback2:hiddenCallBack?
    var type:integer_t?
    weak var locationDelegate:ViewController?
    override init(frame: CGRect, style: UITableViewStyle){
        super.init(frame: frame, style: style)
        var viewAnimation:CAKeyframeAnimation?
        var animationPath:CGMutablePath
        viewAnimation = CAKeyframeAnimation.init(keyPath: "position");
        animationPath = CGMutablePath();
        animationPath.move(to: CGPoint.init(x: frame.size.width/2, y: (UIScreen.main.bounds.size.height + frame.size.height/2)))
        animationPath.addLine(to: CGPoint.init(x: frame.size.width/2, y: (UIScreen.main.bounds.size.height - frame.size.height/2)))

        viewAnimation?.path = animationPath;
        viewAnimation?.duration = 0.2
        viewAnimation?.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseIn)
        viewAnimation?.fillMode = kCAFillModeForwards
        viewAnimation?.isRemovedOnCompletion = false
        self.layer.add(viewAnimation!, forKey: nil)
        self.delegate = self
        self.dataSource = self;
        self.separatorInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
    }
    
//    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        let titleLabel:UILabel?=UILabel.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
//        titleLabel?.text = "  请确认您的打卡地址"
//        titleLabel?.textColor = UIColor.init(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1)
//        titleLabel?.font = systemFontOfpx(px: 48)
//        return titleLabel
//    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "     请确认您的打卡地址"
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    func hiddenView() {
        var viewAnimation:CAKeyframeAnimation?
        var animationPath:CGMutablePath
        viewAnimation = CAKeyframeAnimation.init(keyPath: "position");
        animationPath = CGMutablePath();
        animationPath.move(to: CGPoint.init(x: frame.size.width/2, y: (UIScreen.main.bounds.size.height - frame.size.height/2)))
        animationPath.addLine(to: CGPoint.init(x: frame.size.width/2, y: (UIScreen.main.bounds.size.height + frame.size.height/2)))
        
        viewAnimation?.path = animationPath;
        viewAnimation?.duration = 0.2
        viewAnimation?.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseIn)
        viewAnimation?.delegate = self;
        self.layer.add(viewAnimation!, forKey: nil)

        
    }
    
    
    
    func animationDidStart(_ anim: CAAnimation) {
        print("beginY %d",self.frame.origin.y)
        print("animationStart")
    }
    
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        self.removeFromSuperview()
        self.layer.removeAllAnimations()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        if (dateSource?.count) != nil{
            
            return (dateSource?.count)!;
        }else{
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return lengthOfPx(px: 140)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell");
        let poiInfo = dateSource![indexPath.row]
        if (cell != nil) {
        }else{
            cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
            cell?.textLabel?.font = systemFontOfpx(px: 48)
            cell?.textLabel?.textColor = UIColor.init(red: 102/255.0, green: 102/255.0, blue: 102/255.0, alpha: 1)
            cell?.detailTextLabel?.textColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1)
            cell?.textLabel?.font = systemFontOfpx(px: 36)
            cell?.imageView?.image = UIImage.init(named: "addressPoint")
        }
        cell?.textLabel?.text = (poiInfo as! BMKPoiInfo).name
        cell?.detailTextLabel?.text = (poiInfo as! BMKPoiInfo).address
        
      return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let date:Date?=Date()
        
        let cell = tableView.cellForRow(at: indexPath)
        let myCalendar:NSCalendar = NSCalendar.init(calendarIdentifier:NSCalendar.Identifier.gregorian)!
        myCalendar.locale = Locale.init(identifier: "zh_CN")
        myCalendar.timeZone = NSTimeZone(abbreviation: "EST")! as TimeZone
        myCalendar.timeZone = NSTimeZone(forSecondsFromGMT: +28800) as TimeZone
        
        let compoents:DateComponents = myCalendar.components([NSCalendar.Unit.year,NSCalendar.Unit.month,NSCalendar.Unit.day,NSCalendar.Unit.weekday,NSCalendar.Unit.hour,NSCalendar.Unit.minute,NSCalendar.Unit.second], from: date!)
        let formatter = DateFormatter()
         formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
        
        WTMNetworkTool.shared().request(withUrlstring: "app/attendance/submitAttendance", method: "POST", parameters: ["params":["time":formatter.string(from: date!),"lat":(locationDelegate?.locationServer?.userLocation.location.coordinate.latitude)!,"lng":(locationDelegate?.locationServer?.userLocation.location.coordinate.longitude)!,"address":(cell?.textLabel?.text)!,"type":type!]], success: {[weak self](respone)->Void in
            
            print("\(respone)")
            Tool.error(withMsg: (respone as? NSDictionary)?.object(forKey: "resultMsg") as! String!)
            formatter.dateFormat = "HH:mm"
            
            if self?.callback != nil{self?.callback!(formatter.string(from: date!),(cell?.textLabel?.text)!)}
            if self?.callback2 != nil{self?.callback2!()}
            
            
        }, failure: {(error)->Void in
        
        
        
        })
        
        
        
       
    }

}

//Mark:alertWindow

@available(iOS 10.0, *)
class AlertWindow:UIWindow{
    var addressView:chooseAddressTableView?
    
    override init(frame: CGRect){
      super.init(frame: frame)
        self.backgroundColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 0.3)
        addressView = chooseAddressTableView.init(frame: CGRect.init(x: 0, y: UIScreen.main.bounds.size.height - lengthOfPx(px: 790), width: UIScreen.main.bounds.size.width, height: lengthOfPx(px: 790)), style: UITableViewStyle.plain)
        addressView?.callback2 = {[weak self] in
            self?.addressView?.hiddenView()
            self?.addressView = nil
            self?.windowLevel = -10;
        }
        self.addSubview(addressView!);
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        addressView?.hiddenView()
        addressView = nil;
        self.windowLevel = -100;
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


////Mark:NetWorkClass
//
//class NetWorkTool{
//    class func getServerData(paramter:Dictionary<String, Any>,url:URL,selectorName:String,complet:@escaping (Data?, URLResponse?, Error?)->Void){
//        let session = URLSession.shared;
//        let request = NSMutableURLRequest.init(url: url);
//        
//        request.httpMethod = selectorName;
//        request.httpBody = try!JSONSerialization.data(withJSONObject: paramter, options: JSONSerialization.WritingOptions.prettyPrinted);
//        
//        let dataTask = session.dataTask(with: request as URLRequest, completionHandler:{(data, response, error)->Void in
//            complet(data,response,error);
//        });
//        dataTask.resume();
//    }
//    
//}




